package com.example.customnotification;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {

    private String CHANNEL_ID = "1dr";
    private String CHANNEL_NAME = "namtyje";
    private int NOTIFICATION_ID = 10560;
    private MyBroadcast myBroadcast;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        //创建channel
        if (mNotificationManager.getNotificationChannel(CHANNEL_ID) == null) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, mNotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(false);//显示呼吸灯
            notificationChannel.enableVibration(false);//振动（要权限
            notificationChannel.setLightColor(Color.RED);//灯的颜色
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);//锁屏是否可见
            notificationChannel.setShowBadge(true);
            notificationChannel.setBypassDnd(true);
            //notificationChannel.setSound(null,null);
            notificationChannel.setVibrationPattern(new long[]{Notification.DEFAULT_VIBRATE});//自定义振动new long[]{100, 200, 300, 400}
            //notificationChannel.setDescription(desc);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        //创建通知
        //获取布局
        RemoteViews notificationview = new RemoteViews(getPackageName(), R.layout.notification_layout);
        //修改布局
        notificationview.setTextViewText(R.id.ntf_tv,"被设置过的text");
        //设置点击事件
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,new Intent("click_btn"),0);
        notificationview.setOnClickPendingIntent(R.id.ntf_btn, pendingIntent);
        //设置通知
        Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);
        builder.setSmallIcon(R.drawable.small_notification)
                .setCustomContentView(notificationview)
                .setOngoing(false)
                .build();

        /*
        先
        .setCustomBigContentView(notificationview)
          再           .setCustomContentView(notificationview)
          就可以显示大的了
         */

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送通知
                mNotificationManager.notify(NOTIFICATION_ID, builder.build());
            }
        });


        //注册一个广播
        myBroadcast = new MyBroadcast();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("click_btn");//过滤
        registerReceiver(myBroadcast, intentFilter);//sendBroadcast
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcast);
    }
}